<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class product_detail extends CI_Controller{
	
	public function get_info(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			$codeData = $this->product_code_DAO->getByCode($_POST['product_code_id']);
			if($codeData == null){
				return null;
			} else {
				$this->load->Model("product_detail_DAO");
				$product_code_id = $codeData['id'];
				$code = $_POST['code'];
				$returnData = $this->product_detail_DAO->search($product_code_id, $code);
				if($returnData == null){
					echo null;
				} else {
					$this->load->Model("product_size_DAO");
					$this->load->Model("model/product_detail_search_model");
					$sizeData = $this->product_size_DAO->getById($returnData['product_size_id']);
					$product_detail_search_model = new product_detail_search_model();
					$product_detail_search_model->id = $returnData['id'];
					$product_detail_search_model->vi_name = $returnData['vi_name'];
					$product_detail_search_model->en_name = $returnData['en_name'];
					$product_detail_search_model->retail_price = $returnData['retail_price'];
					$product_detail_search_model->code = $returnData['code'];
					$product_detail_search_model->category_level_1_id = $returnData['category_level_1_id'];
					$product_detail_search_model->category_level_2_id = $returnData['category_level_2_id'];
					$product_detail_search_model->category_level_3_id = $returnData['category_level_3_id'];
					$product_detail_search_model->product_fit_id = $returnData['product_fit_id'];
					$product_detail_search_model->product_crotch_id = $returnData['product_crotch_id'];
					$product_detail_search_model->product_code_id = $returnData['id'];
					$product_detail_search_model->product_size_id = $returnData['id'];
					$product_detail_search_model->description = $returnData['description'];
					$product_detail_search_model->wholesale_price = $returnData['wholesale_price'];
					if($sizeData != null){
						$product_detail_search_model->type_of_size_id = $sizeData['type_of_size_id'];
					}
					echo json_encode($product_detail_search_model);
				}
			}
		} else {
			$this->logout();
		}
	}
	
	public function add(){
		if($this->check_session()){
			$category_level_2_id = $_POST['category_level_2_id'];
			$category_level_3_id = $_POST['category_level_3_id'];
			$product_fit_id = null;
			if(isset($_POST['product_fit_id'])){
				$product_fit_id = $_POST['product_fit_id'] == 0? null : $_POST['product_fit_id'];
			}
			$product_crotch_id = null;
			if(isset($_POST['product_crotch_id'])){
				$product_crotch_id = $_POST['product_crotch_id'] == 0? null : $_POST['product_crotch_id'];
			}
			$this->load->Model("product_code_DAO");
			$product_code = $this->product_code_DAO->getCode($category_level_2_id, $category_level_3_id, $product_fit_id, $product_crotch_id);
			if(isset($product_code)){
				$this->load->Model("product_detail_DAO");
				$code = $_POST['code'];
				if(!$this->product_detail_DAO->checkExist($product_code['id'], $code)){
					$this->load->Model("model/product_detail_model");
					$product_detail_model = new product_detail_model();
					$product_detail_model->vi_name = $_POST['vi_name'];
					$product_detail_model->en_name = $_POST['en_name'];
					$product_detail_model->retail_price = $_POST['retail_price'];
					$product_detail_model->wholesale_price = $_POST['wholesale_price'];
					$product_detail_model->code = $_POST['code'];
					$product_detail_model->category_level_1_id = $_POST['category_level_1_id'];
					$product_detail_model->category_level_2_id = $category_level_2_id;
					$product_detail_model->category_level_3_id = $category_level_3_id;
					$product_detail_model->product_fit_id = $product_fit_id;
					$product_detail_model->product_crotch_id = $product_crotch_id;
					$product_detail_model->product_size_id = $_POST['product_size_id'];
					$product_detail_model->description = $_POST['description'];
					$product_detail_model->product_code_id = $product_code['id'];
					$data = $this->product_detail_DAO->add($product_detail_model);
					echo $data;
				} else {
					echo 'exist';
				}
			} else {
				echo '404';
			}
		} else {
			$this->logout();
		}
    }
	
	public function update(){
		if($this->check_session()){
			$category_level_2_id = $_POST['category_level_2_id'];
			$category_level_3_id = $_POST['category_level_3_id'];
			$product_fit_id = null;
			if(isset($_POST['product_fit_id'])){
				$product_fit_id = $_POST['product_fit_id'] == 0? null : $_POST['product_fit_id'];
			}
			$product_crotch_id = null;
			if(isset($_POST['product_crotch_id'])){
				$product_crotch_id = $_POST['product_crotch_id'] == 0? null : $_POST['product_crotch_id'];
			}
			$this->load->Model("product_code_DAO");
			$product_code = $this->product_code_DAO->getCode($category_level_2_id, $category_level_3_id, $product_fit_id, $product_crotch_id);
			if(isset($product_code)){
				$this->load->Model("product_detail_DAO");
				$code = $_POST['code'];
				$currentProductData = $this->product_detail_DAO->search($product_code['id'], $code);
				if($currentProductData['id'] == null || $currentProductData['id'] == $_POST['id']){
					echo $this->product_detail_DAO->update($_POST['id'], $_POST['vi_name'], $_POST['en_name'], $_POST['retail_price'],
						$_POST['wholesale_price'], $_POST['code'], $_POST['category_level_1_id'], $category_level_2_id,
						$category_level_3_id, $product_fit_id, $product_crotch_id, $product_code['id'], $_POST['product_size_id'], $_POST['description']);
				} else {
					echo 'exist';
				}
			} else {
				echo '404';
			}
		} else {
			$this->logout();
		}
    }
	
	public function delete(){
		if($this->check_session()){
			$this->load->Model("product_detail_DAO");
			echo $this->product_detail_DAO->delete($_POST['id']);
		} else {
			$this->logout();
		}
    }
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}